package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.ExitConsensusUserDao;
import com.sc.nft.dao.SearchDao;
import com.sc.nft.entity.ExitConsensusUser;
import com.sc.nft.entity.Search;
import com.sc.nft.entity.dto.SearchEditDTO;
import com.sc.nft.entity.dto.SearchExistListDTO;
import com.sc.nft.entity.dto.SearchSaveDTO;
import com.sc.nft.entity.dto.SearchStartAndStopDTO;
import com.sc.nft.entity.vo.SearchExistListVO;
import com.sc.nft.entity.vo.SearchListVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.SearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.ObjDoubleConsumer;
import java.util.stream.Collectors;

/**
 * 搜索配置(Search)表服务实现类
 *
 * @author 王伟成
 * @since 2024-03-05 15:23:24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SearchServiceImpl implements SearchService {

    private final SearchDao searchDao;
    private final ExitConsensusUserDao exitConsensusUserDao;
    @Override
    public List<SearchListVO> displayList() {
        return searchDao.displayList();
    }

    @Override
    public List<SearchListVO> displayListForUser(Long userId) {
        ExitConsensusUser exitConsensusUser = exitConsensusUserDao.getByUserId(userId);
        List<SearchListVO> searchListVOS = searchDao.displayList();
        if(ObjectUtil.isNotNull(exitConsensusUser)&&exitConsensusUser.getStatus()){
            return searchListVOS.stream().filter(x->x.getId()!=9L).collect(Collectors.toList());
        }else{
            return searchListVOS.stream().filter(x->x.getId()!=7L).collect(Collectors.toList());
        }
    }

    @Override
    public IPage<SearchExistListVO> existList(SearchExistListDTO searchExistListDTO) {
        return searchDao.existList(searchExistListDTO);
    }
    @Override
    public void save(SearchSaveDTO searchSaveDTO) {
        Search search = BeanUtil.copyProperties(searchSaveDTO, Search.class);
        searchDao.insert(search);
    }
    @Override
    public void edit(SearchEditDTO searchEditDTO) {
        Search search = searchDao.getById(searchEditDTO.getId());
        if (BeanUtil.isEmpty(search)) {
            throw new GlobalRunTimeException("当前修改的搜索项不存在");
        }
        BeanUtil.copyProperties(searchEditDTO,search);
        searchDao.edit(search);
    }
    @Override
    public void startAndStop(SearchStartAndStopDTO searchStartAndStopDTO) {
        searchDao.startAndStop(searchStartAndStopDTO);
    }

}
